//
//  OADefaultCell.m
//  OASDK_Works
//
//  Created by ChenJie on 3/30/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADefaultCell.h"
#import "OASDK.h"

NSString *const addBeforeButtonDidClick2 = @"addBeforeButtonDidClick";

typedef NS_ENUM(NSUInteger, ComponentId) {
    TitleLabel = 1,
    ContentLabel,
};

@interface OADefaultCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation OADefaultCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleLabel.frame = [self.viewModel.oa_layouts[@(TitleLabel)] CGRectValue];
    self.contentLabel.frame = [self.viewModel.oa_layouts[@(ContentLabel)] CGRectValue];
    
//    [self.contentView oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    self.titleLabel.text = nil;
    self.contentLabel.text = nil;
}

#pragma mark - Public methods

#define kTitleFont [UIFont boldSystemFontOfSize:16]
#define kContentFont [UIFont systemFontOfSize:14]
#define kMarginH 15
#define kMarginV 10
#define kSpaceBetweenV 3

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(OADefaultCellViewModel *)viewModel {
    NSMutableDictionary *componentFrames = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    CGFloat leftMark = 0;
    CGFloat rightMark = 0;
    
    topMark += kMarginV;
    leftMark += kMarginH;
    rightMark += kMarginH;
    CGFloat labelWidthMax = cellWidth - leftMark - rightMark;
    CGSize titleSize = [viewModel.title oa_sizeWithWidth:labelWidthMax font:kTitleFont];
    CGRect titleFrame = CGRectMake(leftMark, topMark, titleSize.width, titleSize.height);
    componentFrames[@(TitleLabel)] = OAValueWithRect(titleFrame);
    topMark += titleSize.height;
    
    topMark += kSpaceBetweenV;
    CGSize contentSize = [viewModel.content oa_sizeWithWidth:labelWidthMax font:kContentFont];
    CGRect contentFrame = CGRectMake(leftMark, topMark, contentSize.width, contentSize.height);
    componentFrames[@(ContentLabel)] = OAValueWithRect(contentFrame);
    
    CGFloat cotnentBottom = CGRectGetMaxY(contentFrame);
    
    viewModel.oa_layouts = componentFrames;
    
    return cotnentBottom + kMarginV;
}

#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(OADefaultCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.contentLabel.text = viewModel.content;
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.textColor = OAColor(336699);
    label.numberOfLines = 0;
})

OAGetLabel(contentLabel, ^(UILabel *label){
    label.font = kContentFont;
    label.textColor = OAColor(999999);
    label.numberOfLines = 0;
})

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
